package com.hifar.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class MultiTableSearchUtils {

	public static SearchParamBean getQueryInfo(SearchParamBean query,Map<String, Object> postParam, String... ignoreParam) {
		
		List<String> ignoreCollect = Arrays.stream(ignoreParam).collect(Collectors.toList());

		List<SearchCondition> objList = query.getConditionList();
		SearchCondition obj = null;

		String queryParam = postParam.get("queryParam") == null ? "" : JSON.toJSONString(postParam.get("queryParam"));
		String sortParam = postParam.get("sortParam") == null ? "" : JSON.toJSONString(postParam.get("sortParam"));
		if (queryParam != null && StringUtils.isNotBlank(queryParam)) {
			JSONArray jsonArray = JSON.parseArray(queryParam);
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				String key = jsonObject.getString("key");
				String value = jsonObject.getString("value");
				String match = jsonObject.getString("matchType");
				String dataType = jsonObject.getString("dataType");
				if (!ignoreCollect.contains(key)) {
					if (value != null && StringUtils.isNotBlank(value)) {
						switch (match) {
						case "equal":
							obj = new SearchCondition(toUnderScoreCase(key), "=", value);
							objList.add(obj);
							break;
						case "notEqual":
							obj = new SearchCondition(toUnderScoreCase(key), "!=", value);
							objList.add(obj);
							break;
						case "greaterThan":
							obj = new SearchCondition(toUnderScoreCase(key), ">", value);
							objList.add(obj);
							break;
						case "greaterThanEqual":
							obj = new SearchCondition(toUnderScoreCase(key), ">=", value);
							objList.add(obj);
							break;
						case "lessThan":
							obj = new SearchCondition(toUnderScoreCase(key), "<", value);
							objList.add(obj);
							break;
						case "lessThanEqual":
							obj = new SearchCondition(toUnderScoreCase(key), "<=", value);
							objList.add(obj);
							break;
						case "contain":
							obj = new SearchCondition(toUnderScoreCase(key), "like ", "%" + value + "%");
							objList.add(obj);
							break;
						case "notContain":
							obj = new SearchCondition(toUnderScoreCase(key), " not like ", "%" + value + "%");
							objList.add(obj);
							break;
						case "list":
							obj = new SearchCondition(toUnderScoreCase(key), " in ", value);
							objList.add(obj);
							break;
						case "notList":
							obj = new SearchCondition(toUnderScoreCase(key), " not in ", value);
							objList.add(obj);
						case "range":
							JSONArray objects = JSONArray.parseArray(value);
							if (objects.size() == 2) {
								obj = new SearchCondition(toUnderScoreCase(key), " >= ", objects.get(0).toString());
								objList.add(obj);
								obj = new SearchCondition(toUnderScoreCase(key), " <= ", objects.get(1).toString());
								objList.add(obj);

							}
							break;
						}
					} else {
						continue;
					}
				} else {
					continue;
				}
			}
		}
		if (sortParam != null && StringUtils.isNotBlank(sortParam)) {
			List<String> orderList=query.getOrderStringList();
			JSONArray jsonArray = JSON.parseArray(sortParam);
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				String key = jsonObject.getString("key");
				String value = jsonObject.getString("value");
				if (!ignoreCollect.contains(key)) {
					if (value != null && StringUtils.isNotBlank(value)) {
						if (value.equals("2")) {
							orderList.add(toUnderScoreCase(key)+" asc ");
						} else if (value.equals("3")) {
							orderList.add(toUnderScoreCase(key)+" desc ");
						}
					}
				}
			}
			if (orderList.size() > 0) {
				query.setOrderStringList(orderList);
			}
			
		}
		query.setConditionList(objList);
		return query;
	}

	private static String toUnderScoreCase(String camelCaseStr) {
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < camelCaseStr.length(); i++) {
			char c = camelCaseStr.charAt(i);
			if (Character.isUpperCase(c)) {
				if (i > 0) {
					builder.append('_');
				}
				builder.append(Character.toLowerCase(c));
			} else {
				builder.append(c);
			}
		}
		return builder.toString();
	}
}
